﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using DiscoveryLogic.DataServices.DataSources;
using System.Collections.Generic;
using DiscoveryLogic.UI.Layouts.Converters;

namespace DiscoveryLogic.UI.Layouts.Controls
{
    public partial class BillGraph : UserControl
    {
        #region Private Variables

            private const double graphTop = 75;
            private const double max = 170;
            private const int numBars = 10;
            private const double flagPaymentPercent = 0.35;

        #endregion

        #region Constructors

            public BillGraph()
            {
                InitializeComponent();
               
            }

        #endregion

        #region Public Methods

            // Used to draw the graph given a list of bill payments
            public void SetGraph(List<BillPayment> history, string payeeName)
            {
                // Set Payee name
                
                PayeeName.SetValue(TextBlock.TextProperty, payeeName);

                // Set Bar Heights
                double[] paymentAmounts = new double[numBars];
                double totalPayment = 0;
                double maxPayment = 0;
                double avgPayment = 0;

                // Find Max
                // ASSUMPTION: latest payments are in the back of the list
                for (int i = 0; i < numBars; i++)
                {
                    // Get payment amount at i
                    paymentAmounts[i] = history[history.Count - (numBars - i)].getPaymentAmount();

                    // Keep track of largest payment
                    if (paymentAmounts[i] > maxPayment){
                        maxPayment = paymentAmounts[i];
                    }

                    // Keep track of total sum of payments
                    totalPayment += paymentAmounts[i];
                }

                // Find Average
                avgPayment = totalPayment / numBars;

                // Set height/colors of bars
                for (int i = 0; i < numBars; i++)
                {
                    // Calculate bar height
                    double barHeight = (paymentAmounts[i] / maxPayment) * max;

                    // Find bar i and set height
                    Rectangle bar = Root.FindName("Bar" + i) as Rectangle;
                    bar.SetValue(Rectangle.HeightProperty, barHeight);
                    bar.SetValue(Canvas.TopProperty, ( max - barHeight));

                    // If bar are +/- flagPaymentPercent of the average payment,
                    // then turn the bar red
                    if (paymentAmounts[i] > avgPayment * (1 + flagPaymentPercent)
                     || paymentAmounts[i] < avgPayment - (avgPayment * flagPaymentPercent))
                    {
                        // Find gradient stops for this bar
                        GradientStop gs0 = Root.FindName("Bar" + i + "Gradient0") as GradientStop;
                        GradientStop gs1 = Root.FindName("Bar" + i + "Gradient1") as GradientStop;
                        GradientStop gs2 = Root.FindName("Bar" + i + "Gradient2") as GradientStop;
                        /*
                        GradientStop gs3 = topCanvas.FindName("Bar" + i + "Gradient3") as GradientStop;
                        GradientStop gs4 = topCanvas.FindName("Bar" + i + "Gradient4") as GradientStop;
                        GradientStop gs5 = topCanvas.FindName("Bar" + i + "Gradient5") as GradientStop;
                        */

                        LinearGradientBrush gradientBrush = Root.FindName("Bar" + i + "Brush") as LinearGradientBrush;

                        // Set color of gradient stops
                        gs0.Color = Utils.HexToColor("#FFFFE3A6");
                        gs1.Color = Utils.HexToColor("#FFFF9D66");
                        gs2.Color = Utils.HexToColor("#FFD00000");
                        gs2.Offset = 1;

                        /*
                        // Remove unwanted gradient stops
                        gradientBrush.GradientStops.Remove(gs3);
                        gradientBrush.GradientStops.Remove(gs4);
                        gradientBrush.GradientStops.Remove(gs5);
                         */
                         
                    }
                    else
                    {
                        // Set the bar blue again
                        // Find gradient stops for this bar
                        GradientStop gs0 = Root.FindName("Bar" + i + "Gradient0") as GradientStop;
                        GradientStop gs1 = Root.FindName("Bar" + i + "Gradient1") as GradientStop;
                        GradientStop gs2 = Root.FindName("Bar" + i + "Gradient2") as GradientStop;
                        /*
                        GradientStop gs3 = Root.FindName("Bar" + i + "Gradient3") as GradientStop;
                        GradientStop gs4 = Root.FindName("Bar" + i + "Gradient4") as GradientStop;
                        GradientStop gs5 = Root.FindName("Bar" + i + "Gradient5") as GradientStop;
                        */

                        LinearGradientBrush gradientBrush = Root.FindName("Bar" + i + "Brush") as LinearGradientBrush;

                        // Set color of gradient stops
                        gs0.Color = Utils.HexToColor("#FFA0CAEB");
                        gs1.Color = Utils.HexToColor("#FF83B7E2");
                        gs2.Color = Utils.HexToColor("#FF0000D0");
                        gs2.Offset = 1;

                        /*
                        // Remove unwanted gradient stops
                        gradientBrush.GradientStops.Remove(gs3);
                        gradientBrush.GradientStops.Remove(gs4);
                        gradientBrush.GradientStops.Remove(gs5);
                        */
                    }
                    
                    // Set Dates Textblocks
                    TextBlock DateText = Root.FindName("Date_" + i) as TextBlock;
                    DateText.SetValue(TextBlock.TextProperty, history[i].getDatePaid().ToShortDateString());
                }

                // Set Max, Avg Textblocks
                MaxAmount.SetValue(TextBlock.TextProperty, 
                    string.Format((string)Resources["Res_MaxAmount"], Math.Round(maxPayment).ToCurrency()));
                MinAmount.SetValue(TextBlock.TextProperty, 
                    string.Format((string)Resources["Res_MinAmount"], ((double)0).ToCurrency()));
                    
                AvgAmount.SetValue(Canvas.TopProperty, max - (max * (avgPayment / maxPayment)) + graphTop);
                AvgAmount.SetValue(TextBlock.TextProperty, 
                    string.Format((string)Resources["Res_AverageAmount"], Math.Round(avgPayment).ToCurrency()));
            }

        #endregion

        #region Event Handlers

            private void CloseButton_Clicked(object sender, MouseButtonEventArgs e)
            {
                if (closePopup != null)
                {
                    closePopup(this, e);
                }
            }

        #endregion

        #region Events

            public event MouseButtonEventHandler closePopup;

        #endregion

    }
}
